using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace BHXH.Repository
{
    public enum HinhThucKeToan
    {
        NhatKyChung,
        ChungTuGhiSo,
        NhatKyChungTu
    }

    [Serializable]
    public class LocalSettings
    {
        public static LocalSettings HeThong = LocalSettings.LoadFromFile();

        private static string _ConnectString = "";
        public static bool NeedReloadConnectString = true;
        public static string ConnectString
        {
            get
            {
                if (NeedReloadConnectString)
                {
                    if (NeedReloadConnectString)
                    {
                        LocalSettings tthh = LocalSettings.LoadFromFile();
                        if (tthh.UseExpertMode)
                        {
                            _ConnectString = tthh.DB_ConnectionString;
                        }
                        else
                        {
                            _ConnectString = string.Format("SERVER = {0}; DATABASE = {1}; UID= {2}; PWD={3}",
                                                                tthh.DB_Server, tthh.DB_Name, tthh.DB_UID, tthh.DB_PASS);
                        }
                        NeedReloadConnectString = false;
                    }
                    //_ConnectString = string.Format("SERVER = .; DATABASE = BHXH2012; UID= sa; PWD=srmdep");
                    //NeedReloadConnectString = false;
                }
                return _ConnectString;
            }
        }

        public bool UseExpertMode;
        public string DB_Server = "";
        public string DB_UID = "";
        public string DB_PASS = "";
        public string DB_Name = "";
        public string DB_ConnectionString = "";
        public string TenTruyCap = "";
        public string Password = "";
        public bool GhiLai = false;
        public bool AutoLogin = true;

        #region Mot so thong so mac dinh
        public Guid KhoID = Guid.Empty;
        public Guid ThueSuatID = Guid.Empty;
        public Guid PhuongThucThanhToanMuaHangID = Guid.Empty;
        public Guid PhuongThucThanhToanBanHangID = Guid.Empty;
        #endregion

        #region Thong tin cua hang in o hoa don ban le
        public string TenCuaHang = "";
        public string DiaChi = "";
        public string DienThoai = "";
        #endregion

        public int FormWidth = 0;
        public int FormHeight = 0;
        public int FormTop = 0;
        public int FormLeft = 0;
        public bool FormMaximized = false;
        public string SkinName = "";
        public string _NumbericFormatString = "";
        public string NumbericFormatString
        {
            get
            {
                return "### ### ### ### ###.#";
            }
            set
            {
                _NumbericFormatString = value;
            }
        }

        public string NumberGroupSeparator = ".";
        public string NumberDecimalSeparator = ",";
        public HinhThucKeToan HinhThucKeToan = HinhThucKeToan.NhatKyChung;

        public string SoMay = "";
        public string HardwareID = "";

        public string SerialKey;
        public DateTime HanSuDung = new DateTime(2000, 1, 1);

        public void Save2File()
        {
            try
            {
                // Delete old file
                string fname = CommonLib.ThuMucGoc + "\\Settings.dat";
                if (System.IO.File.Exists(fname)) System.IO.File.Delete(fname);

                System.IO.MemoryStream ms = new MemoryStream();

                // write new information to new file
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, this);
                string encryptedText = CommonLib.EncryptBytes2String(ms.GetBuffer(), "abc");

                StreamWriter sw = new StreamWriter(fname);
                sw.Write(encryptedText);
                sw.Close();

                NeedReloadConnectString = true;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static LocalSettings LoadFromFile()
        {
            LocalSettings LocalSettings = null;
            try
            {
                if (System.IO.File.Exists(CommonLib.ThuMucGoc + "\\Settings.dat"))
                {
                    StreamReader sr = new StreamReader(CommonLib.ThuMucGoc + "\\Settings.dat");
                    string encryptedText = sr.ReadToEnd();
                    sr.Close();
                    byte[] buffer = CommonLib.DecryptString2Bytes(encryptedText, "abc");
                    MemoryStream ms = new MemoryStream(buffer);

                    BinaryFormatter formatter = new BinaryFormatter();
                    object obj = formatter.Deserialize(ms);

                    if (obj == null)
                        LocalSettings = new LocalSettings();
                    else
                    {
                        LocalSettings = (LocalSettings)obj;
                    }
                }
                else LocalSettings = new LocalSettings();
                LocalSettings.NumbericFormatString = "###.###,";
            }
            catch
            {
                LocalSettings = new LocalSettings();
            }
            return LocalSettings;
        }
    }
}
